package com.foreknow.controller;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

public class FirstServlet extends HttpServlet {
    //通过初始化方法可以从配制文件web.xml中获取一些信息
    @Override
    public void init(ServletConfig config) throws ServletException {
        //获取局部初始化参数
        //String pageSize = config.getInitParameter("pageSize"); //"15"
        //获取全局的初始化参数
        String pageSize = config.getServletContext().getInitParameter("pageSize");
        int num = Integer.parseInt(pageSize);
        System.out.println(num);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //服务器向客户端响应的内容的类型
        resp.setContentType("text/html;charset=utf-8");
        //通过getWriter方法获取PrintWriter输出流对象，主要用于在网页上输出结果
        PrintWriter out = resp.getWriter();
        out.println("<html>");
        out.println("<head><title>servlet</title></head>");
        out.println("<body>");
        //返回请求的 HTTP 方法的名称，例如，GET、POST。
        out.println(req.getMethod() + "<br>");
        //从协议名称直到 HTTP 请求的第一行的查询字符串中，返回该请求的 URL 的一部分。
        out.println(req.getRequestURI() + "<br>");
        //获取上下文路径
        out.println(req.getContextPath() + "<br>");
        out.println(new Date());
        out.println("</body>");
        out.println("</html>");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
